package v1

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"next-vue-backend/model"
	"next-vue-backend/utils/errmsg"
	"strconv"
)

// AddVerse 新增诗词
func AddVerse (c *gin.Context) {
	var data model.Verse
	_ = c.ShouldBindJSON(&data)
	code = model.CreateVerse(&data)
	c.JSON(http.StatusOK, gin.H{
		"status": code,
		"data": data,
		"message":errmsg.GetErrMsg(code),
	})
}


// GetVerseList 查看诗词列表
func GetVerseList (c *gin.Context) {
	pageSize, _ := strconv.Atoi(c.Query("pagesize"))
	pageNum, _ := strconv.Atoi(c.Query("pagenum"))

	if pageSize == 0 {
		pageSize = -1
	}

	if pageNum == 0 {
		pageNum = -1
	}

	data, code, total := model.GetVerses(pageSize, pageNum)
	c.JSON(http.StatusOK, gin.H{
		"status": code,
		"data": data,
		"total": total,
		"message": errmsg.GetErrMsg(code),
	})
}

// GetRandomVerse 查看随机单条诗词
func GetRandomVerse (c *gin.Context) {
	data, code := model.GetRandomVerse()
	c.JSON(http.StatusOK, gin.H{
		"status": code,
		"data": data,
		"message": errmsg.GetErrMsg(code),
	})
}

// EditVerse 编辑诗词
func EditVerse (c *gin.Context) {
	var data model.Verse
	c.ShouldBindJSON(&data)
	id, _ := strconv.Atoi(c.Param("id"))
	code = model.EditVerse(id, &data)

	c.JSON(http.StatusOK,gin.H{
		"status": code,
		"message": errmsg.GetErrMsg(code),
	})
}

// DeleteVerse 删除诗词
func DeleteVerse (c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	code = model.DeleteVerse(id)
	c.JSON(http.StatusOK, gin.H{
		"status": code,
		"message": errmsg.GetErrMsg(code),
	})
}